package com.haiyi.software.entity;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.jivesoftware.smack.packet.DefaultPacketExtension;
import org.jivesoftware.smack.packet.Message;
import org.xmlpull.v1.XmlPullParser;

import android.util.Log;
import android.util.Xml;

import com.haiyi.software.app.MainApplication;
import com.haiyi.software.app.R;

public class EntityMsgGif extends EntityMsg {

	protected String gif;

	public String getGif() {

		int dot = gif.indexOf('.');
		if ((dot > -1) && (dot < (gif.length() - 1))) {

			// Log.i("GIF", gif.substring(0, dot));
			return gif.substring(0, dot);
		} else {
			return gif;
		}
	}

	public void setGif(String gif) {
		this.gif = gif + ".gif";
	}

	@Override
	public Message getPacket() {

		UserInfo myInfo = MainApplication.getInstance().getMineUserInfo();

		String strServerIP = MainApplication.getInstance().getServerIP();

		String XMPPID = this.yUID + "@" + strServerIP + "/Android";

		Message msg = new Message(XMPPID, Message.Type.chat);

		DefaultPacketExtension DeExtension = new DefaultPacketExtension(
				"MyExtension", "jabber:client");

		DeExtension.setValue("name", myInfo.getUname());

		DeExtension.setValue("username", myInfo.getUsername());

		DeExtension.setValue("uhead", myInfo.getHeadSmall());

		DeExtension.setValue("gif", this.gif);// gif 就是表情动画的名称。

		msg.setBody(body);

		msg.addExtension(DeExtension);

		return msg;
	}

	@Override
	public int getMessageCategory() {
		// TODO 自动生成的方法存根
		return MESSAGECATEGORY_GIF;
	}

	@Override
	public void setAllMessage(String allMessage) {

		this.allMessage = allMessage;
		InputStream xmlInStream = new ByteArrayInputStream(
				allMessage.getBytes());

		XmlPullParser pull = Xml.newPullParser();

		try {
			pull.setInput(xmlInStream, "utf-8");

			int type = pull.getEventType();

			while (type != XmlPullParser.END_DOCUMENT) {

				switch (type) {
				case XmlPullParser.START_DOCUMENT:
					// entMsg.setAllMessage(str_xml);
					break;
				case XmlPullParser.START_TAG:

					if ("message".equals(pull.getName())) {

						this.setMessageID(pull.getAttributeValue(null, "id"));

					} else if ("body".equals(pull.getName())) {

						this.setBody(pull.nextText());

					} else if ("uhead".equals(pull.getName())) {

						this.setUhead(pull.nextText());

					} else if ("name".equals(pull.getName())) {

						this.setName(pull.nextText());

					} else if ("gif".equals(pull.getName())) {

						this.gif = pull.nextText();

					} else if ("username".equals(pull.getName())) {

						this.setUsername(pull.nextText());

					}
					break;
				case XmlPullParser.END_DOCUMENT:

					break;
				}
				type = pull.next();

			}
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

}
